package com.rzjm.exercise.module.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rzjm.exercise.common.base.controller.BaseController;
import com.rzjm.exercise.common.base.data.DataTableLayUI;
import com.rzjm.exercise.common.log.annotation.Log;
import com.rzjm.exercise.common.utils.DateUtils;
import com.rzjm.exercise.common.utils.R;
import com.rzjm.exercise.common.utils.StringUtils;
import com.rzjm.exercise.module.domain.*;
import com.rzjm.exercise.module.service.*;
import com.rzjm.exercise.sys.system.user.domain.UserDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/module/eventsRecordRewards", produces = "application/json;charset=UTF-8")
public class EventsRecordRewardsController extends BaseController {
    @Resource
    private EventsRecordRewardsService eventsRecordRewardsService;
    @Resource
    private EventsRecordImgService eventsRecordImgService;
    @Resource
    private TownsService townsService;
    @Resource
    private ShopService shopService;
    @Value("${projecturl.imgurl}")
    private String imgurl;

    @Log("查询奖惩台账页面")
    @GetMapping("")
    @RequiresPermissions("module:eventsRecordRewards:eventsRecordRewards")
    String eventsRecordRewards(Model model) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar firstDay = Calendar.getInstance();
        firstDay.set(Calendar.DAY_OF_MONTH, 1);
        firstDay.set(Calendar.HOUR_OF_DAY, 0);
        firstDay.set(Calendar.MINUTE, 0);
        model.addAttribute("beginTime", simpleDateFormat.format(firstDay.getTime()));
        model.addAttribute("endTime", DateUtils.getDay());
        UserDO user = getUser();
        QueryWrapper<Towns> qwTown = new QueryWrapper<>();
        qwTown.eq("parent_id", 0);
        qwTown.eq("district_id", 1319);
        List<Towns> townsList = townsService.list(qwTown);
        QueryWrapper<Shop> wrapper = new QueryWrapper<>();
        wrapper.eq("state", 1);
        wrapper.ne("shop_type", 3);
        if (user.getTownId() != null && user.getTownId() > 0) {
            wrapper.eq("town_id", user.getTownId());
        }
        List<Shop> shopList = shopService.list(wrapper);
        model.addAttribute("townsList", townsList);
        model.addAttribute("shopList", shopList);
        model.addAttribute("user", user);
        return "module/eventsRecordRewards/list";
    }


    /**
     * 删除
     */
    @Log("删除奖惩台账")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("module:eventsRecordRewards:remove")
    public R remove(Integer id) {
        if (eventsRecordRewardsService.removeById(id)) {
            return R.ok();
        }
        return R.error();
    }

    @Log("奖惩台账详情页面")
    @GetMapping("/showDetail")
    @RequiresPermissions("module:eventsRecordRewards:edit")
    String showDetail(String sysUserId, String beginTime,String endTime,Model model) {
        model.addAttribute("sysUserId", sysUserId);
        model.addAttribute("beginTime", beginTime);
        model.addAttribute("endTime", endTime);
        return "module/eventsRecordRewards/detail";
    }

    @Log("查询奖惩台账页面")
    @ResponseBody
    @GetMapping("/list")
    public DataTableLayUI<EventsRecordRewards> list(DataTableLayUI<EventsRecordRewards> condition) {
        Map<String, Object> searchParams = condition.getSearchParams();
        if (searchParams.get("beginTime") != null && StringUtils.isNotBlank(searchParams.get("beginTime").toString())) {
            searchParams.put("beginTime", searchParams.get("beginTime").toString() + " 00:00:00");
        }
        if (searchParams.get("endTime") != null && StringUtils.isNotBlank(searchParams.get("endTime").toString())) {
            searchParams.put("endTime", searchParams.get("endTime").toString() + " 23:59:59");
        }
        DataTableLayUI<EventsRecordRewards> dt = eventsRecordRewardsService.getListPage(condition);
        List<EventsRecordRewards> list = dt.getData();
        for (int i = 0; i < list.size(); i++) {
            List<EventsRecordImg> imgList = new ArrayList<>();
            List<EventsRecordImg> handleImgList = new ArrayList<>();
            EventsRecordRewards rewards = list.get(i);
            QueryWrapper<EventsRecordImg> wrapper = new QueryWrapper<>();
            wrapper.eq("record_id", rewards.getRecordId());
            List<EventsRecordImg> allImgList = eventsRecordImgService.list(wrapper);
            for (int j = 0; j < allImgList.size(); j++) {
                EventsRecordImg recordImg = allImgList.get(j);
                recordImg.setImgUrl(imgurl + recordImg.getImgUrl());
                if (recordImg.getImgType() == 0) {
                    imgList.add(recordImg);
                } else {
                    handleImgList.add(recordImg);
                }
            }
            rewards.setImgList(imgList);
            rewards.setHandleImgList(handleImgList);
        }
        return dt;
    }

    @Log("查询奖惩台账页面")
    @ResponseBody
    @GetMapping("/userList")
    public DataTableLayUI<EventsRecordRewards> userList(DataTableLayUI<EventsRecordRewards> condition) {
        Map<String, Object> searchParams = condition.getSearchParams();
        if (searchParams.get("beginTime") != null && StringUtils.isNotBlank(searchParams.get("beginTime").toString())) {
            searchParams.put("beginTime", searchParams.get("beginTime").toString() + " 00:00:00");
        }
        if (searchParams.get("endTime") != null && StringUtils.isNotBlank(searchParams.get("endTime").toString())) {
            searchParams.put("endTime", searchParams.get("endTime").toString() + " 23:59:59");
        }
        return eventsRecordRewardsService.getUserListPage(condition);
    }
}
